import { globalSetting } from '@/constant'

/**
 * window.localStorage
 * @method setItem 设置
 * @method getItem 获取
 * @method removeItem 移除
 * @method clear 移除全部
 */
export const LocalStorage = {
  setItem(key: string, val: any) {
    const storageKey = `${globalSetting.storagePreName}-${key}`
    window.localStorage.setItem(storageKey, JSON.stringify(val))
  },
  getItem(key: string) {
    const storageKey = `${globalSetting.storagePreName}-${key}`
    const json: any = window.localStorage.getItem(storageKey)
    return JSON.parse(json)
  },
  removeItem(key: string) {
    const storageKey = `${globalSetting.storagePreName}-${key}`
    window.localStorage.removeItem(storageKey)
  },
  clear() {
    window.localStorage.clear()
  },
}

/**
 * window.sessionStorage
 * @method setItem 设置会话缓存
 * @method getItem 获取会话缓存
 * @method removeItem 移除会话缓存
 * @method clear 移除全部会话缓存
 */
export const SessionStorage = {
  setItem(key: string, val: any) {
    const storageKey = `${globalSetting.storagePreName}-${key}`
    window.sessionStorage.setItem(storageKey, JSON.stringify(val))
  },
  getItem(key: string) {
    const storageKey = `${globalSetting.storagePreName}-${key}`
    const json: any = window.sessionStorage.getItem(storageKey)
    return JSON.parse(json)
  },
  removeItem(key: string) {
    const storageKey = `${globalSetting.storagePreName}-${key}`
    window.sessionStorage.removeItem(storageKey)
  },
  clear() {
    window.sessionStorage.clear()
  },
}
